﻿/*
 
Celerity: Sensory Overload
Copright 2012 by Adam Hill, David Gannon, Thomas Tamblyn & Patrick Ytting
Contact: adam.hill@outlook.com
Website: adamhill.org
Licence: MS-Pl Licence
  
 */

using Celerity.UI.Definition;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using System;

namespace Celerity.Entities
{
    public class UIEntityItem
    {
        float centreOffsetX;
        float rightAlignOffsetX;
        Vector2 offset = new Vector2();
        Vector2 size = new Vector2();
        Rectangle rect = new Rectangle();

        public UIDrawCondition DrawCondition { private get; set; }
        public Texture2D ImageDefault { private get; set; }
        public Texture2D ImagePressed { private get; set; }
        public SpriteFont Font { private get; set; }
        public Color FontColorDefault { private get; set; }
        public Color FontColorPressed { private get; set; }
        public string Text { private get; set; }
        public bool ApplyCentring { private get; set; }
        public bool ApplyRightAlign { private get; set; }
        public bool IsPressed { private get; set; }
        public bool IsDynamicScore { get; set; }

        public Texture2D CurrentImage
        {
            get
            {
                return IsPressed ? ImagePressed : ImageDefault;
            }
        }

        public Vector2 Offset
        {
            get
            {
                return offset;
            }
            set
            {
                offset = value; RecalcRect();
            }
        }

        public Vector2 Size
        {
            get
            {
                return size;
            }
            set
            {
                size = value; RecalcRect();
            }
        }

        public Rectangle Rect
        {
            get
            {
                return rect;
            }
            set
            {
                rect.X = value.X;
                rect.Y = value.Y;
                rect.Width = value.Width;
                rect.Height = value.Height;
            }
        }

        string TextSource
        {
            get
            {
                int ms = GlobalGameStates.PlayerAliveMilliseconds;
                TimeSpan ts = TimeSpan.FromMilliseconds(GlobalGameStates.PlayerAliveMilliseconds);
                string score = String.Format("{0:D2}:{1:D2}", ts.Minutes, ts.Seconds);
                return IsDynamicScore ? score : Text;

            }
        }

        void RecalcRect()
        {
            rect.X = (int)(Offset.X);
            rect.Y = (int)(Offset.Y);
            rect.Width = (int)(Size.X);
            rect.Height = (int)(Size.Y);
        }

        public void Update()
        {
            if (Font != null)
            {
                centreOffsetX = (Size.X - Font.MeasureString(TextSource ?? string.Empty).X) / 2f;
                rightAlignOffsetX = (Size.X - Font.MeasureString(TextSource ?? string.Empty).X);
            }
        }

        public void Draw(SpriteBatch spriteBatch)
        {
            if (DrawCondition != null && !DrawCondition.IsValid)
            {
                return;
            }

            if (CurrentImage != null)
            {
                spriteBatch.Draw(CurrentImage, Rect, Color.White);
            }

            if (Font != null && FontColorDefault != null)
            {
                var centredPosition = Offset + new Vector2(centreOffsetX, 0);
                var rightAlignPosition = Offset + new Vector2(rightAlignOffsetX, 0);
                var actualPosition = ApplyCentring ? centredPosition : (ApplyRightAlign ? rightAlignPosition : Offset);
                Color textColor = IsPressed ? FontColorPressed : FontColorDefault;
                spriteBatch.DrawString(Font, TextSource ?? string.Empty, actualPosition, textColor);
            }
        }
    }
}
